package com.lwt.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwt.yygh.common.result.Result;
import com.lwt.yygh.common.utils.MD5;
import com.lwt.yygh.model.hosp.HospitalSet;
import com.lwt.yygh.hosp.service.HospitalSetService;
import com.lwt.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @Author Weton Li
 * @Date 2021/4/4 23:07
 */

@Api("全国医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;

    /**
     * 查询全国医院列表信息
     *
     * @return
     */
    @ApiOperation("查询医院列表")
    @GetMapping("getList") // Restful风格get查询请求
    public Result getList() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 逻辑删除全国某id医院信息
     *
     * @param id
     * @return
     */
    @ApiOperation("通过id移除医院")
    @DeleteMapping("{id}") // 浏览器默认发送get请求，此时发送delete请求：Request method 'GET' not supported
    public Result deleteById(@PathVariable("id") Integer id) {
        boolean isRemove = hospitalSetService.removeById(id);
        if (isRemove) {
            return Result.ok(); // 这里逻辑删除成功 不传参
        } else {
            return Result.fail();
        }
    }

    /**
     * 模糊+条件查询 前端输入的信息以json数据格式 传给服务器将json数据转化为bean对象
     * 若使用@RequestBody注解，必使用post请求
     * 这里不用配置分页插件配置类？
     *
     * @param current            当前页
     * @param limit              每页数量
     * @param hospitalSetQueryVo 前端对象类数据
     * @return
     */
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit); // 创建当前对象，传递当前页，每页记录数
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>(); // 查询构造器 构造条件

        String hosname = hospitalSetQueryVo.getHosname();

        String hoscode = hospitalSetQueryVo.getHoscode();

        if (!StringUtils.isEmpty(hosname)) {

            wrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {

            wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
        }
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper); // mp封装好的page()方法
        return Result.ok(pageHospitalSet); // 封装结果集返回固定json数据格式
    }

    /**
     * 保存医院信息
     * @param hospitalSet
     * @return
     */
    @PostMapping("addHospSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){

        hospitalSet.setStatus(1); // 默认状态不可用0，设置状态为可用1

        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));//Message Digest

        boolean isSave = hospitalSetService.save(hospitalSet);

        if (isSave) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据id获取一条医院信息
     * @param id
     * @return
     */
    @GetMapping("getHospSetById/{id}")
    public Result getHospSetById(@PathVariable("id") Long id){
        HospitalSet hostById = hospitalSetService.getById(id);

//        int i = 1/0;
        return Result.ok(hostById);
    }

    /**
     * 一定是前端接受前端传来的json数据将其转化为json对象 再去更新，而不是根据id更新，因为已经根据id查询出一条bean，
     * 只是做类似于添加的保存（update)操作！！
     * @param hospitalSet
     * @return
     */
    @PostMapping("updateHospSetById")
    public Result updateHospSetById(@RequestBody HospitalSet hospitalSet){

        boolean isUpdate = hospitalSetService.updateById(hospitalSet);
        if (isUpdate) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据集合中的id，批量删除多个医院信息，前端使用数组传递
     * @param idList
     * @return
     */
    @DeleteMapping("deleteHospSetBatch")
    public Result deleteHospSetBatch(@RequestBody List<Long> idList){
        boolean isBatchRemove = hospitalSetService.removeByIds(idList);
        if (isBatchRemove) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 更改医院status状态码，锁、解锁上传医院信息
     * @param id
     * @param status
     * @return
     */
    @PutMapping("lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id,
                              @PathVariable Integer status){

        HospitalSet hospById = hospitalSetService.getById(id);
        hospById.setStatus(status);
        hospitalSetService.updateById(hospById);
        return Result.ok();
    }

    /**
     * 给联系人发送短信，他通过医院编号和医院名去对接医院
     * @param id
     * @return
     */
    @PutMapping("sendSignKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospById = hospitalSetService.getById(id);
        String hoscode = hospById.getHoscode();
        String hosname = hospById.getHosname();

        //TODO 发送信息
        return Result.ok();
    }
}
